<?php
require_once ('../../simpletest/autorun.php');

class TestETC_replaceFileContent extends UnitTestCase {
    
    function findFilesStartWith($targetDir,$needle){
        
        $files = array();
        if ($handle = opendir($targetDir)) {
        
            /* This is the correct way to loop over the directory. */
        
            while (false !== ($file = readdir($handle))) {
                if(false !== strpos($file, $needle)){
                    $files[] = $file;
                }        
            }
        
            closedir($handle);
        }
        return $files;
    }
    
    function replaceFileContent($filename){

        $data = file_get_contents($filename);
        
        //find get and replace it with process
        //$data = str_replace("get", "process", $data);
        //processOneself, processOneself
        $data = str_replace("processSession", "getSession", $data);
        $data = str_replace("processOneself", "getOneself", $data);

        return file_put_contents($filename, $data); 
    }    
    
    //refer to http://www.simpletest.org/en/unit_test_documentation.html
    function testFind(){ 
        $targetDir = 'F:\wamp\www\o-my-friends\test';
        $needle = "1234";
        $result = self::findFilesStartWith($targetDir,$needle);
        
        $this->assertNotNull($result);
        $this->assertTrue(count($result) == 2);
        
        $needle = "TestFunc_process";
        $result = self::findFilesStartWith($targetDir,$needle);
        
        $this->assertNotNull($result);
        $this->assertTrue(count($result) == 23);
        
    }
    
    function testReplace(){

        $targetDir = 'F:\wamp\www\o-my-friends\test';
        $needle = "TestFunc_process";
        
        $files = self::findFilesStartWith($targetDir, $needle);
        foreach($files as $file){
            $this->assertTrue(self::replaceFileContent($targetDir."\\".$file));
        }
        
    }
    
}






?>
